package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import logic.EasyFileChooser;
import logic.Controller;
import qw.easyFrame.enums.SelectionMode;
import qw.easyFrame.view.util.SwingUtil;

/**
 * 主窗口.
 * @author Rex
 *
 */
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 20240921L;
	
	/**
	 * 选中的文件.
	 */
	private static File[] chosen;
	
	/**
	 * 文件格式下拉框的首选项文字.
	 */
	public static String box_text = "选择文本编码 (默认自动识别)";
	
	/**
	 * 待用户输入要搜索的文字的文本框内的默认文字.
	 */
	public static String field0_text = "输入要搜索的文字……";
	
	/**
	 * 待用户输入要搜索的文件格式的文本框内的默认文字.
	 */
	public static String field1_text = "输入要搜索的文件格式 (用空格分开, 默认txt doc docx xls xlsx)";

	/**
	 * @param width
	 * @param height
	 */
	private MainFrame(int width, int height, int fontStyle) {
		super("文本批量搜索工具");
		
		Font font = new Font("Microsoft yahei", fontStyle, 12);
		
		JComboBox<String> box = new JComboBox<>();
		box.setFont(font);
		box.addItem(box_text);
		box.addItem("UTF-8");
		box.addItem("GBK");
		box.addItem("ISO-8859-1");
		box.addItem("GB2312");
		box.addItem("ASCII");
		box.addItem("UTF-16");
		box.addItem("GB18030");
		box.addItem("BIG5");
		box.addItem("UTF-32");
		box.addItem("GB12345-90");
		box.addItem("Shift_JIS");
		box.addItem("EUC-JP");
		box.addItem("ISO-2022-JP");
		
		JTextField field0 = SwingUtil.defaultJTextField(field0_text, font);
		JTextField field1 = SwingUtil.defaultJTextField(field1_text, font);
		
		JCheckBox check0 = new JCheckBox("忽略大小写");
		check0.setFont(font);
		JCheckBox check1 = new JCheckBox("星号模糊匹配");
		check1.setFont(font);
		JCheckBox check2 = new JCheckBox("穿透子文件夹");
		check2.setFont(font);
		check2.setSelected(true);
		JCheckBox check3 = new JCheckBox("统计文字出现次数");
		check3.setFont(font);
		JPanel checkBoxes = SwingUtil.defaultJPanel(1, 1, 0, 0);
		checkBoxes.add(check0);
		checkBoxes.add(check1);
		checkBoxes.add(check2);
		checkBoxes.add(check3);
		
        JButton btn0 = SwingUtil.defaultJButton("选择文件", font);
        JButton btn1 = SwingUtil.defaultJButton("开始搜索", font);
        
        EasyTextPane area = new EasyTextPane(font);
        JScrollPane scroll = new JScrollPane(area); 

		JPanel panel0 = SwingUtil.defaultJPanel(1, 2, 0, 0);
		panel0.add(btn0);
		panel0.add(btn1);
		JPanel panel1 = SwingUtil.defaultJPanel(1, 2, 0, 0);
		panel1.add(box);
		panel1.add(panel0);
		JPanel panel2 = SwingUtil.defaultJPanel(4, 1, 0, 0);
		panel2.add(panel1);
		panel2.add(checkBoxes);
		panel2.add(field0);
		panel2.add(field1);
		
		Container cp = this.getContentPane();
        cp.add(panel2, BorderLayout.NORTH);
        cp.add(scroll, BorderLayout.CENTER);
        
        area.defaultRightClickMenu(font);
		
		EasyFileChooser chooser = new EasyFileChooser(area, SelectionMode.FILES_AND_DIRS);
		chooser.setFont(font);
		btn0.addActionListener(e -> chosen = chooser.choose());
		
		btn1.addActionListener(e -> Controller.run(chosen, box, field0, field1, area, check0, check1, check2, check3));
		
		SwingUtil.setJFrame(this, width, height);
	}
	
	public static void main(String[] args) {
		int width = 576;
		int height = 864;
		int fontStyle = Font.BOLD;
		try {
			System.err.close();
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
			UIManager.put("RootPane.setupButtonVisible", false);
		} catch (Throwable e) {
			// 当软件运行环境不支持BeautyEye时，弃用BeautyEye，改用Java Swing默认外观
			width = 500;
			height = 750;
			fontStyle = Font.PLAIN;
		}
		
		new MainFrame(width, height, fontStyle);
	}
	
	/**
	 * 测试不同启动方式用.
	 * @param args
	 */
	public static void main2(String[] args) {
		int width = 500;
		int height = 750;
		int fontStyle = Font.PLAIN;
		
		new MainFrame(width, height, fontStyle);
	}

}